﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
using gts;

namespace IndustryWrapper
{
    public enum EGtsCardType
    {
        CTS400=1,
        GTS800=2
    }
    public enum EGtsIoType
    {
        ACC1616D=1,//16的IO模块
        ACC0604A=2//模拟量模块
    }
    /// <summary>
    /// 扩展模块的SHUJU
    /// </summary>
    public class CCfgIoDataGts
    {
        public int id = 0;//模块地址
        public EGtsIoType ioType = EGtsIoType.ACC1616D;
    }
    /// <summary>
    /// 
    /// </summary>
    public class CCfgDataGts
    {
        public EGtsCardType cardType = EGtsCardType.CTS400;//卡类型GTS400，GTS800 只考虑PG
        public string cardConfigeFn = "GTS400.CFG";//这张卡的配置文件名称
        public string extIoFn = "";//扩展模块的配置文件,如果="",代表没有扩展模块.
        //分为 ACC1616D ACC0603
        public List<CCfgIoDataGts> extIoTypes = new List<CCfgIoDataGts>();//IO类型列表
    }
    /// <summary>
    /// GTS的配置组网结构
    /// </summary>
    public class CCfgGts:CMotionConfigeBase
    {
        public List<CCfgDataGts> cfg = new List<CCfgDataGts>();
        
        /// <summary>
        /// 根据硬件配置表导出轴列表打csv中 JIXU:
        /// 方便开发人员编辑
        /// </summary>
        /// <param name="csv">文件名</param>
        /// <returns></returns>
        public override bool exportAxisToCsv(string csv)
        {
            //生成数据先.
            List<CAxisSinglePrm> axis = new List<CAxisSinglePrm>();
            CAxisRef.idCount = 0;
            int cardNum = 0;
            int axisNum = 0;
            CAxisSinglePrm tmp = null;
            foreach (CCfgDataGts item in cfg)
            {
                switch (item.cardType)
                {
                    case EGtsCardType.CTS400:
                        //创建轴
                        axisNum = 0;
                        for (int i = 0; i < 4; i++)
                        {
                            tmp = new CAxisSinglePrm();
                            tmp.cardNum = (short)cardNum;
                            tmp.axisNum =(short)(++axisNum);
                            axis.Add(tmp);
                        }
                        cardNum++;
                        break;
                    case EGtsCardType.GTS800:
                        //创建轴
                        axisNum = 0;
                        for (int i = 0; i < 8; i++)
                        {
                            tmp = new CAxisSinglePrm();
                            tmp.cardNum = (short)cardNum;
                            tmp.axisNum = (short)(++axisNum);
                            axis.Add(tmp);
                        }
                        cardNum++;
                        break;
                }
            }
            //保存数据到csv
            FileInfo fi = new FileInfo(csv);
            if (!fi.Directory.Exists)
            {
                fi.Directory.Create();
            }
            FileStream fs = new FileStream(csv, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            //StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
            string data = "ID(数字必须唯一),名称,类型(0=直线1=旋转),最大速度unit/s,最大加速度G RAD/S,节距MM/RAD,分辨率Plus,回零方式COE402,回零快速,回零慢速,回零偏移unit,卡号/核号,轴号";
            sw.WriteLine(data);
            //保存为csv
            foreach (CAxisSinglePrm item in axis)
            {

            }
            return true;
        }
        /// <summary>
        /// 导出硬件配置中的IO到io列表中
        /// 方便开发人员修改IO列表
        /// </summary>
        /// <param name="csv"></param>
        /// <returns></returns>
        public override bool exportIoToCsv(string csv)
        {
            //List<string> data = new List<string>();
            //CIoModelPrm prm = new CIoModelPrm();
            //int cardNum = 0;
            //long modelSign = 0;
            //data.Add("模块名称,模块识别号XXXX_YYYY_ZZZZ,读取数据起始Index,全局ID(全部唯一, 用来映射),类型(输入, 输出, 模拟量输入, 模拟量输出，编码器, 比较输出),引脚号,名称,正常/取反");
            //foreach (CGtsCfgData item in cfg)
            //{
            //    switch (item.cardType)
            //    {
            //        case EGtsCardType.CTS400:
            //            //创建本地IO
            //            data.Add("4轴本地IO");
            //            prm.CardIDOrModelID = 1;
            //            //创建扩展模块的
            //            foreach (CGtsIoCfgData item2 in item.extIoTypes)
            //            {
            //                switch (item2.ioType)
            //                {
            //                    case EGtsIoType.ACC1616D:
            //                        tmpModel = new CIoModel();
            //                        ioModel.Add(tmpModel);
            //                        break;
            //                    case EGtsIoType.ACC0604A:
            //                        tmpModel = new CIoModel();
            //                        ioModel.Add(tmpModel);
            //                        break;
            //                }
            //            }
            //            break;
            //        case EGtsCardType.GTS800:
            //            //创建本地IO
            //            tmpModel = new CIoModel(CIoModelType.I16O16);
            //            ioModel.Add(tmpModel);
            //            //创建扩展模块的
            //            foreach (CGtsIoCfgData item2 in item.extIoTypes)
            //            {
            //                switch (item2.ioType)
            //                {
            //                    case EGtsIoType.ACC1616D:
            //                        tmpModel = new CIoModel(CIoModelType.I16O16);
            //                        ioModel.Add(tmpModel);
            //                        break;
            //                    case EGtsIoType.ACC0604A:
            //                        tmpModel = new CIoModel(CIoModelType.AD6DA4);
            //                        ioModel.Add(tmpModel);
            //                        break;
            //                }
            //            }
            //            break;
            //    }
            //}
            //保存为csv格式
            return true;
        }

        public override bool importFromCsv(string csv)
        {
            throw new NotImplementedException();
        }
        public override bool importFromXml(string xml)
        {
            //先读取类型
            XmlDocument doc = new XmlDocument();
            doc.Load(xml);
            XmlNodeList nds = doc.SelectNodes("/Hard/Hard");
            if (nds == null)
                return false;
            foreach (XmlNode item in nds)
            {
                CCfgDataGts data = new CCfgDataGts();
                foreach (XmlNode item2 in item.ChildNodes)
                {
                    switch (item2.Name)
                    {
                        case "ConfigeFile":
                            data.cardConfigeFn = item2.InnerText;
                            break;
                        case "CardType":
                            string cardTypeStr = item2.InnerText;
                            if (cardTypeStr.IndexOf("400") >=0 )
                            {
                                data.cardType = EGtsCardType.CTS400;
                            }
                            else if (cardTypeStr.IndexOf("800") >= 0)
                            {
                                data.cardType = EGtsCardType.GTS800;
                            }
                            break;
                        case "IoModel":
                            if (item2.ChildNodes.Count>0)
                            {
                                foreach (XmlNode item3 in item2.ChildNodes)
                                {
                                    switch (item3.Name)
                                    {
                                        case "ConfigeFile":
                                            data.extIoFn = item3.InnerText;
                                            break;
                                        case "Model":
                                            CCfgIoDataGts io = new CCfgIoDataGts();
                                            string id = item3.Attributes["id"].Value;
                                            io.id = int.Parse(id);
                                            string ioTypeStr = item3.InnerText.ToUpper();
                                            if (ioTypeStr.IndexOf("1616") >=0)
                                            {
                                                io.ioType = EGtsIoType.ACC1616D;
                                            }
                                            else if (ioTypeStr.IndexOf("0604") >= 0)
                                            {
                                                io.ioType = EGtsIoType.ACC0604A;
                                            }
                                            data.extIoTypes.Add(io);
                                            break;
                                    }
                                }
                            }
                            break;
                    }
                }
                cfg.Add(data);
            }
            return true;
        }
    }
    /// <summary>
    /// 固高卡的封装  都在这里
    /// 数据区域都在这个类里面
    /// 每次项目  这个类需要重写的
    /// </summary>
    public class CMotion_GTS:IMotionPartI, IIoPart, IInitModel, IFreshModel
    {
        public bool initOk = false;
        private CCfgGts cfg = null;

        private Thread runThread = null;       //用来刷新IO的
        //私有变量
        private short cardNum = 0;
        //局部变量
        public CMotion_GTS(CCfgGts cfg)
        {
            this.cfg = cfg;
        }

        public int MC_Home(ref CAxisRef axis)
        {
            mc.THomePrm prm = new mc.THomePrm();
            axis.homed = 0;
            axis.isHoming = false;
            short rtn = mc.GT_GetHomePrm(axis.prm.cardNum, axis.prm.axisNum, out prm);
            rtn = mc.GT_ZeroPos(axis.prm.cardNum, axis.prm.axisNum, 1);
            rtn = mc.GT_ClrSts(axis.prm.cardNum, axis.prm.axisNum, 1);
            prm.mode = axis.prm.homeType;
            prm.acc = axis.prm.getAccPlsPerMs2();
            prm.dec = prm.acc;
            if (axis.prm.homeSearch >= 0)
            {
                prm.moveDir = 1;
            }
            else
            {
                prm.moveDir = -1;
            }
            prm.indexDir = 1;
            prm.edge = 0;
            prm.homeOffset = (int)axis.prm.mm2pls(axis.prm.homeoffset);
            prm.velHigh = axis.prm.mmpers2plsperms(axis.prm.homeVel1);
            prm.velLow = axis.prm.mmpers2plsperms(axis.prm.homeVel2);
            prm.searchHomeDistance = Math.Abs((int)axis.prm.mm2pls(axis.prm.homeSearch));
            prm.searchIndexDistance = prm.searchHomeDistance;
            prm.escapeStep = (int)axis.prm.mm2pls(axis.prm.homeLeave);
            rtn += mc.GT_GoHome(axis.prm.cardNum, axis.prm.axisNum, ref prm);
            if (rtn == 0) axis.isHoming = true;
            else axis.isHoming = false;
            return rtn;
        }

        public int MC_MoveAbs(CAxisRef axis, double tpos, double beilv)
        {
            short rtn = 0;
            int pmode;
            int psts;
            uint pclock;
            rtn += mc.GT_GetPrfMode(axis.prm.cardNum, axis.prm.axisNum, out pmode, 1, out pclock);
            rtn += mc.GT_GetSts(axis.prm.cardNum, axis.prm.axisNum, out psts, 1, out pclock);
            if ((psts & 0x400) == 0)
            {
                if (pmode != 0)
                {
                    rtn += mc.GT_PrfTrap(axis.prm.cardNum, axis.prm.axisNum);
                }
                mc.TTrapPrm trapprm = new mc.TTrapPrm();
                trapprm.acc = axis.prm.getAccPlsPerMs2();
                trapprm.dec = axis.prm.getAccPlsPerMs2();
                trapprm.smoothTime = 50;
                trapprm.velStart = 0;
                rtn += mc.GT_SetTrapPrm(axis.prm.cardNum, axis.prm.axisNum, ref trapprm);
                rtn += mc.GT_SetVel(axis.prm.cardNum, axis.prm.axisNum, axis.prm.getVelPlsPerMs() * beilv);
                rtn += mc.GT_SetPos(axis.prm.cardNum, axis.prm.axisNum, (int)(axis.prm.mm2pls(tpos)));
                //int sts = 0;
                //uint clock = 0;
                //rtn = mc.GT_GetSts(cardnum, relAxis, out sts, 1, out clock);
                rtn += mc.GT_Update(axis.prm.cardNum, 1 << (axis.prm.axisNum - 1));
            }
            else if (pmode == 0)
            {
                rtn += mc.GT_SetVel(axis.prm.cardNum, axis.prm.axisNum, axis.prm.getVelPlsPerMs() * beilv);
                rtn += mc.GT_SetPos(axis.prm.cardNum, axis.prm.axisNum, (int)(axis.prm.mm2pls(tpos)));
                rtn += mc.GT_Update(axis.prm.cardNum, 1 << (axis.prm.axisNum - 1));
            }
            return rtn;
        }

        public int MC_MoveAdd(CAxisRef axis, double dist, double beilv)
        {
            short rtn = 0;
            int pmode;
            int psts;
            uint pclock;
            double prfpos;
            rtn += mc.GT_GetPrfMode(axis.prm.cardNum, axis.prm.axisNum, out pmode, 1, out pclock);
            rtn += mc.GT_GetSts(axis.prm.cardNum, axis.prm.axisNum, out psts, 1, out pclock);
            if ((psts & 0x400) == 0)
            {
                if (pmode != 0)
                {
                    rtn += mc.GT_PrfTrap(axis.prm.cardNum, axis.prm.axisNum);
                }
                mc.TTrapPrm trapprm = new mc.TTrapPrm();
                trapprm.acc = axis.prm.getAccPlsPerMs2();
                trapprm.dec = axis.prm.getAccPlsPerMs2();
                trapprm.smoothTime = 50;
                trapprm.velStart = 0;
                rtn += mc.GT_SetTrapPrm(axis.prm.cardNum, axis.prm.axisNum, ref trapprm);
                rtn += mc.GT_SetVel(axis.prm.cardNum, axis.prm.axisNum, axis.prm.getVelPlsPerMs() * beilv);
                rtn += mc.GT_GetPrfPos(axis.prm.cardNum, axis.prm.axisNum, out prfpos, 1, out pclock);
                rtn += mc.GT_SetPos(axis.prm.cardNum, axis.prm.axisNum, (int)(axis.prm.mm2pls(dist) + prfpos));
                rtn += mc.GT_Update(axis.prm.cardNum, 1 << (axis.prm.axisNum - 1));
            }
            return rtn;
        }

        public int MC_MoveJog(CAxisRef axis, double beilv)
        {
            try
            {
                short rtn = 0;
                int pmode;
                int psts;
                uint pclock;
                rtn += mc.GT_GetPrfMode(axis.prm.cardNum, axis.prm.axisNum, out pmode, 1, out pclock);
                rtn += mc.GT_GetSts(axis.prm.cardNum, axis.prm.axisNum, out psts, 1, out pclock);
                if ((psts & 0x400) == 0)
                {
                    if (pmode != 1)
                    {
                        rtn = mc.GT_PrfJog(axis.prm.cardNum, axis.prm.axisNum);
                    }
                    mc.TJogPrm jogprm = new mc.TJogPrm();
                    jogprm.acc = axis.prm.getAccPlsPerMs2();
                    jogprm.dec = axis.prm.getAccPlsPerMs2();
                    jogprm.smooth = 0.5;
                    rtn = mc.GT_SetJogPrm(axis.prm.cardNum, axis.prm.axisNum, ref jogprm);
                    rtn = mc.GT_SetVel(axis.prm.cardNum, axis.prm.axisNum, axis.prm.getVelPlsPerMs() * beilv);
                    rtn = mc.GT_ClrSts(axis.prm.cardNum, axis.prm.axisNum, 1);
                    rtn = mc.GT_Update(axis.prm.cardNum, 1 << (axis.prm.axisNum - 1));
                }
                else if (pmode == 1)
                {
                    rtn = mc.GT_SetVel(axis.prm.cardNum, axis.prm.axisNum, axis.prm.getVelPlsPerMs() * beilv);
                    rtn = mc.GT_ClrSts(axis.prm.cardNum, axis.prm.axisNum, 1);
                    rtn = mc.GT_Update(axis.prm.cardNum, 1 << (axis.prm.axisNum - 1));
                }
                return rtn;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public int MC_PowerOn(CAxisRef axis)
        {
            return mc.GT_AxisOn(axis.prm.cardNum, axis.prm.axisNum);
        }

        public int MC_Reset(CAxisRef axis)
        {
            mc.GT_ClrSts(axis.prm.cardNum, axis.prm.axisNum, 1);
            if (axis.alarm)
            {
                mc.GT_SetDoBitReverse(axis.prm.cardNum, mc.MC_CLEAR, axis.prm.axisNum, 0, 500);
            }
            return 0;
        }

        public bool setDo(CIO_Do output, bool value, bool reverse = false)
        {
            if (value)
            {
                // mc.GT_SetDoBit((short)CIoPrms.o[output.adress].modelNum, mc.MC_GPO, (short)(CIoPrms.o[output.adress].index+1),0);
            }
            else
            {
                //mc.GT_SetDoBit((short)CIoPrms.o[output.adress].modelNum, mc.MC_GPO, (short)(CIoPrms.o[output.adress].index+1), 1);
            }
            return true;
        }
        private void subStatusData()
        {
            //short rtn = 0;
            //int inValue = 0, outValue = 0;
            //uint clock = 0;
            //double[] prfpos = new double[8];
            //double[] encpos = new double[8];
            //double[] encvel = new double[8];
            ////辅助编码器
            //double[] fzencpos = new double[2];
            //double[] fzencvel = new double[2];
            //int[] sts = new int[8];
            //rtn = mc.GT_GetDi(0, mc.MC_GPI, out inValue);
            //rtn = mc.GT_GetDo(0, mc.MC_GPO, out outValue);
            //rtn = mc.GT_GetPrfPos(0, 1, out prfpos[0], 4, out clock);
            //rtn = mc.GT_GetEncPos(0, 1, out encpos[0], 4, out clock);
            //rtn = mc.GT_GetEncVel(0, 1, out encvel[0], 4, out clock);
            //rtn = mc.GT_GetSts(0, 1, out sts[0], 4, out clock);
            ////辅助编码器
            //rtn = mc.GT_GetEncPos(0, 9, out fzencpos[0], 2, out clock);
            //rtn = mc.GT_GetEncVel(0, 9, out fzencvel[0], 2, out clock);
            ////解析
            //for (int i = 0; i < 16; i++)
            //{
            //    mdis[i] = ((1 << i) & inValue) == 0;
            //    mdos[i] = ((1 << i) & outValue) == 0;
            //    if (i < 4)//轴状态分解
            //    {
            //        axisRefs[i].alarm = (sts[i] & 0x2) != 0;
            //        axisRefs[i].servoOn = (sts[i] & 0x200) != 0;
            //        axisRefs[i].limitN = (sts[i] & 0x40) != 0;
            //        axisRefs[i].limitP = (sts[i] & 0x20) != 0;
            //        axisRefs[i].moving = (sts[i] & 0x400) != 0;

            //        axisRefs[i].cmdPos = (float)AxisParameters.prms[i].pls2mm((long)prfpos[i]);
            //        axisRefs[i].relPos = (float)AxisParameters.prms[i].pls2mm((long)encpos[i]);
            //        axisRefs[i].relVel = (float)AxisParameters.prms[i].plsperms2mmpers(encvel[i]);
            //    }
            //    if (i == 4)//辅助
            //    {
            //        axisRefs[i].relPos = (float)AxisParameters.prms[i].pls2mm((long)fzencpos[i - 4]);
            //        axisRefs[i].relVel = (float)AxisParameters.prms[i].plsperms2mmpers(fzencvel[i - 4]);
            //        axisRefs[i].alarm = false;
            //        axisRefs[i].servoOn = false;
            //        axisRefs[i].limitN = false;
            //        axisRefs[i].limitN = false;
            //        axisRefs[i].moving = axisRefs[i].relVel != 0;
            //    }
            //}
        }

        public int MC_AxisRef(ref CAxisRef axisref)
        {
            short rtn = 0;
            uint clock = 0;
            double[] prfpos = new double[1];
            double[] encpos = new double[1];
            double[] encvel = new double[1];
            int[] sts = new int[1];
            rtn += mc.GT_GetPrfPos(0, 1, out prfpos[0], 1, out clock);
            rtn += mc.GT_GetEncPos(0, 1, out encpos[0], 1, out clock);
            rtn += mc.GT_GetEncVel(0, 1, out encvel[0], 1, out clock);
            rtn += mc.GT_GetSts(0, 1, out sts[0], 1, out clock);
            //解析
            axisref.alarm = (sts[0] & 0x2) != 0;
            axisref.servoOn = (sts[0] & 0x200) != 0;
            axisref.limitN = (sts[0] & 0x40) != 0;
            axisref.limitP = (sts[0] & 0x20) != 0;
            axisref.moving = (sts[0] & 0x400) != 0;

            axisref.cmdPos = (float)axisref.prm.pls2mm((long)prfpos[0]);
            axisref.relPos = (float)axisref.prm.pls2mm((long)encpos[0]);
            axisref.relVel = (float)axisref.prm.plsperms2mmpers(encvel[0]);

            return rtn;
        }

        public int MC_EStop(CAxisRef axis)
        {
            axis.isHoming = false;
            mc.GT_Stop(axis.prm.cardNum, 1 << (axis.prm.axisNum - 1), 1 << (axis.prm.axisNum - 1));
            return 0;
        }

        public int MC_PowerOff(CAxisRef axis)
        {
            return mc.GT_AxisOff(axis.prm.cardNum, axis.prm.axisNum);
        }

        
        

        public int MC_SetPos(CAxisRef axis, double pos)
        {
            mc.GT_SetPrfPos(axis.prm.cardNum, axis.prm.axisNum, (int)pos);
            mc.GT_SetEncPos(axis.prm.cardNum, axis.prm.axisNum, (int)pos);
            mc.GT_SynchAxisPos(axis.prm.cardNum, 1 << (axis.prm.axisNum - 1));
            return 0;
        }

        //int IMotionPart5.MC_StartCapture(AxisRef enc, CCapturePrm capturePrm)
        //{
        //    //CAPTURE_HOME(该宏定义为 1) Home 捕获
        //    //CAPTURE_INDEX(该宏定义为 2) Index 捕获
        //    //CAPTURE_PROBE(该宏定义为 3) 探针捕获
        //    //CAPTURE_HSIO0(该宏定义为 6) HSIO0 口捕获
        //    //CAPTURE_HSIO1(该宏定义为 7) HSIO1 口捕获
        //    //short rtn = mc.GT_ClrSts((short)prm.cardnum, (short)prm.axisnum, 1);
        //    short rtn = mc.GT_SetCaptureMode(enc.prm.cardNum, enc.prm.axisNum, (short)capturePrm.type);
        //    return rtn;
        //}

        //int IMotionPart5.MC_CaptureStatus(AxisRef enc, ref double capturePos)
        //{
        //    short cptSts = 0;
        //    int value = 0;
        //    uint clock = 0;
        //    short rtn = mc.GT_GetCaptureStatus(enc.prm.cardNum, enc.prm.axisNum, out cptSts, out value, 1, out clock);
        //    if (rtn == 0 && cptSts == 1)
        //    {
        //        capturePos = enc.prm.pls2mm(value);
        //        return 0;
        //    }
        //    else
        //    {
        //        return 1;
        //    }
        //}

        //int IMotionPart5.MC_CrdCreate(int crdNum)
        //{
        //    if (cardNum < 0 || cardNum >= AxisParameters.crdS.Count) return -1;
        //    short rtn = 0;
        //    int pmode;
        //    int psts;
        //    uint pclock;
        //    CCrdPrm crdPrm = AxisParameters.crdS[crdNum];
        //    //随便找一个轴来查看下是否是差不坐标系
        //    rtn += mc.GT_GetPrfMode((short)crdPrm.cardNum, (short)crdPrm.x, out pmode, 1, out pclock);
        //    rtn += mc.GT_GetSts((short)crdPrm.cardNum, (short)crdPrm.x, out psts, 1, out pclock);
        //    if ((psts & 0x400) == 0)
        //    {
        //        mc.TCrdPrm prm = new mc.TCrdPrm();
        //        prm.dimension = (short)crdPrm.dimension;
        //        prm.evenTime = 5;
        //        prm.setOriginFlag = 1;
        //        prm.synVelMax = crdPrm.getCrdMaxVelPlsPerMs();
        //        prm.synAccMax = crdPrm.getCrdMaxAccPlsPerMs2();
        //        switch (crdPrm.x)
        //        {
        //            case 1:
        //                prm.profile1 = 1;
        //                break;
        //            case 2:
        //                prm.profile2 = 1;
        //                break;
        //            case 3:
        //                prm.profile3 = 1;
        //                break;
        //            case 4:
        //                prm.profile4 = 1;
        //                break;
        //            case 5:
        //                prm.profile5 = 1;
        //                break;
        //            case 6:
        //                prm.profile6 = 1;
        //                break;
        //            case 7:
        //                prm.profile7 = 1;
        //                break;
        //            case 8:
        //                prm.profile8 = 1;
        //                break;
        //            default:
        //                break;
        //        }
        //        switch (crdPrm.y)
        //        {
        //            case 1:
        //                prm.profile1 = 2;
        //                break;
        //            case 2:
        //                prm.profile2 = 2;
        //                break;
        //            case 3:
        //                prm.profile3 = 2;
        //                break;
        //            case 4:
        //                prm.profile4 = 2;
        //                break;
        //            case 5:
        //                prm.profile5 = 2;
        //                break;
        //            case 6:
        //                prm.profile6 = 2;
        //                break;
        //            case 7:
        //                prm.profile7 = 2;
        //                break;
        //            case 8:
        //                prm.profile8 = 2;
        //                break;
        //            default:
        //                break;
        //        }
        //        switch (crdPrm.z)
        //        {
        //            case 1:
        //                prm.profile1 = 3;
        //                break;
        //            case 2:
        //                prm.profile2 = 3;
        //                break;
        //            case 3:
        //                prm.profile3 = 3;
        //                break;
        //            case 4:
        //                prm.profile4 = 3;
        //                break;
        //            case 5:
        //                prm.profile5 = 3;
        //                break;
        //            case 6:
        //                prm.profile6 = 3;
        //                break;
        //            case 7:
        //                prm.profile7 = 3;
        //                break;
        //            case 8:
        //                prm.profile8 = 3;
        //                break;
        //            default:
        //                break;
        //        }
        //        switch (crdPrm.a)
        //        {
        //            case 1:
        //                prm.profile1 = 4;
        //                break;
        //            case 2:
        //                prm.profile2 = 4;
        //                break;
        //            case 3:
        //                prm.profile3 = 4;
        //                break;
        //            case 4:
        //                prm.profile4 = 4;
        //                break;
        //            case 5:
        //                prm.profile5 = 4;
        //                break;
        //            case 6:
        //                prm.profile6 = 4;
        //                break;
        //            case 7:
        //                prm.profile7 = 4;
        //                break;
        //            case 8:
        //                prm.profile8 = 4;
        //                break;
        //            default:
        //                break;
        //        }
        //        rtn += mc.GT_SetCrdPrm((short)crdPrm.cardNum, (short)crdPrm.crdNum, ref prm);
        //        rtn += mc.GT_CrdClear((short)crdPrm.cardNum, (short)crdPrm.crdNum, 0);
        //        rtn += mc.GT_CrdClear((short)crdPrm.cardNum, (short)crdPrm.crdNum, 1);
        //        return rtn;
        //    }
        //    return -1;
        //}

        public void Fresh()
        {
            subStatusData();
        }

        public void Run()
        {
            IFreshModel fresh = this as IFreshModel;
            while (true)
            {
                fresh.Fresh();
                Thread.Sleep(1);
            }
        }

        public bool Init()
        {
            try
            {
                short rtn = 0;
                cardNum = 0;
                foreach (CCfgDataGts item in cfg.cfg)
                {
                    rtn += mc.GT_Open(cardNum, 0, 0);
                    rtn += mc.GT_Reset(cardNum);
                    rtn += mc.GT_LoadConfig(cardNum, item.cardConfigeFn);
                    switch (item.cardType)
                    {
                        case EGtsCardType.CTS400:
                            rtn += mc.GT_ClrSts(cardNum, 1, 4);
                            rtn += mc.GT_ZeroPos(cardNum, 1, 4);
                            break;
                        case EGtsCardType.GTS800:
                            rtn += mc.GT_ClrSts(cardNum, 1, 8);
                            rtn += mc.GT_ZeroPos(cardNum, 1, 8);
                            break;
                    }
                    //有IO模块
                    if (item.extIoFn != "")
                    {
                        rtn += mc.GT_OpenExtMdl(cardNum, "gts");
                        rtn += mc.GT_ResetExtMdl(cardNum);
                        rtn += mc.GT_LoadExtConfig(cardNum, item.extIoFn);
                    }
                    cardNum++;
                }
                //if (rtn == 0)
                //{
                //    runThread = new System.Threading.Thread(Run);
                //    runThread.IsBackground = true;
                //    runThread.Start();
                //    return initOk = true;
                //}
                return initOk = (rtn == 0);
            }
            catch (Exception)
            {
                return initOk = false;
            }
        }

        public bool UnInit()
        {
            try
            {
                short rtn = 0;
                short cardNum = 0;
                foreach (CCfgDataGts item in cfg.cfg)
                {
                    //有IO模块
                    if (item.extIoFn != "")
                    {
                        rtn += mc.GT_ResetExtMdl(cardNum);
                        rtn += mc.GT_CloseExtMdl(cardNum);
                    }
                    //关闭卡
                    rtn += mc.GT_Reset(cardNum);
                    rtn += mc.GT_Close(cardNum);
                    cardNum++;
                }
                //停止刷新
                if (runThread != null && runThread.IsAlive)
                {
                    runThread.Abort();
                    Thread.Sleep(5);
                    runThread = null;
                }
                rtn += mc.GT_Reset(cardNum);
                rtn += mc.GT_Close(cardNum);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool getDi(int startIndex, int lenth, ref bool[] value)
        {
            throw new NotImplementedException();
        }

        public bool getDo(int startIndex, int lenth, ref bool[] value)
        {
            throw new NotImplementedException();
        }

        public int MC_AxisRef(int startIndex, int lenth, ref CAxisRef[] axisS)
        {
            throw new NotImplementedException();
        }

        //int IMotionPart5.MC_CrdSetOrigin(int crdNum, List<double> origionData)
        //{
        //    throw new NotImplementedException();
        //}

        //int IMotionPart5.MC_CrdOverride(int crdNum, float BeiLv)
        //{
        //    throw new NotImplementedException();
        //}

        public int MC_HomeStatus(ref CAxisRef axis)
        {
            short rtn = 0;
            mc.THomeStatus tmpHomeSts = new mc.THomeStatus();
            rtn = mc.GT_GetHomeStatus(axis.prm.cardNum, axis.prm.axisNum, out tmpHomeSts);
            if (tmpHomeSts.run == 0 && tmpHomeSts.error == mc.HOME_ERROR_NONE)//回零完成
            {
                axis.homed = 1;
                mc.GT_ZeroPos(axis.prm.cardNum, axis.prm.axisNum, 1);
                axis.isHoming = false;
            }
            if (tmpHomeSts.run == 0 && tmpHomeSts.error != mc.HOME_ERROR_NONE)//回零完成
            {
                axis.homed = -1;
                axis.isHoming = false;
            }
            return rtn;
        }

        public bool getDiCounter(CIO_Di input, ref long counter)
        {
            throw new NotImplementedException();
        }

        public int MC_Compare(CAxisRef axis, double posMM, CIO_Di cmpInput)
        {
            throw new NotImplementedException();
        }

        public bool getDi(ref CIO_Di input)
        {
            throw new NotImplementedException();
        }

        public bool getDo(ref CIO_Do output)
        {
            throw new NotImplementedException();
        }

        public bool getIoModel(ref CIoModel model)
        {
            throw new NotImplementedException();
        }

        public int MC_SetAxisParameter(CAxisSinglePrm axisPrm)
        {
            throw new NotImplementedException();
        }

        public bool MC_GetIO(ref CIOBase data)
        {
            throw new NotImplementedException();
        }

        public bool MC_SetDo(ref CIOBase data,object value)
        {
            throw new NotImplementedException();
        }

        public bool MC_GetModel(CIoModel data)
        {
            throw new NotImplementedException();
        }
         
        public bool MC_GetIO(ref CIO_Di data)
        {
            throw new NotImplementedException();
        }

        public bool MC_GetIO(CIO_Do data)
        {
            throw new NotImplementedException();
        }

        public float MC_GetIO(CIO_Ai data)
        {
            throw new NotImplementedException();
        }

        public float MC_GetIO(CIO_Ao data)
        {
            throw new NotImplementedException();
        }

        public long MC_GetIO(CIO_Enc data)
        {
            throw new NotImplementedException();
        }

        public bool MC_SetDo(CIOBase data, object value)
        {
            throw new NotImplementedException();
        }

        public bool MC_SetDo(CIO_Do data, bool value)
        {
            throw new NotImplementedException();
        }

        public bool MC_SetDo(CIO_Ao data, float value)
        {
            throw new NotImplementedException();
        }

        public int MC_AxisRef(CAxisModelPrm prm)
        {
            throw new NotImplementedException();
        }
    }
}
